package com.example.parsing.producer;

import com.alibaba.fastjson.JSONObject;
import com.example.parsing.unit.NoGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class MyProducer {

    private static final Logger logger = LoggerFactory.getLogger(MyProducer.class);
    public static Map<String,String> idMap = new ConcurrentHashMap<>(); // 存放消息id+deviceName


    /**
     * esn: 目前后台生成，保证唯一性
     * type: 设备的类型
     * name: deviceName 目前是设备的型号
     * deviceTitle 目前后台自动生成描述类型的
     */
    public void register(String esn, String type, String name , String deviceTitle, KafkaTemplate<String,String> kafkaTemplate){

        logger.info("****************************准备注册"+System.currentTimeMillis());
        try{
            JSONObject request = new JSONObject();
            JSONObject registerJson = new JSONObject();
            String uuid = NoGenerator.getUUID();
            registerJson.put("id",uuid);
            registerJson.put("requestTime",String.valueOf(NoGenerator.getDate()));
            request.put("serialNumber",esn);
            request.put("status",2);
            request.put("modelName",type);
            request.put("deviceName",name);
            request.put("deviceTitle",deviceTitle);
            registerJson.put("request", request);
            logger.info("----------------------------------------已经成功发送，发送的消息是=" + registerJson.toJSONString());
            kafkaTemplate.send("sys." + type + ".subdevice.register.request", type, registerJson.toJSONString());
            idMap.put(uuid,name);
        }catch (Exception e){
            logger.error("----------------------------------------注册失败，错误消息是"+e);
        }
    }
}
